package tool;

import java.awt.Color;
import java.sql.Timestamp;
import java.util.*;

import com.base.bean.*;
import com.base.dao.*;
import com.base.service.*;


/**
 * 图片处理类：
 * 通过mediaId下载图片  打水印  重命名文件名
 * 
 * tempPath：临时文件存放地
 * localPath：本地文件存放地
 * 
 * @author Administrator
 *
 */
public class ImageToolFinal {
	
	
	// 单例模式 start
	private volatile static ImageToolFinal instance = null;
	private static String XD_Path;
	private static String DK_Path;
	private static String LocalPath;
	
	private static String _url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
	
	
	private ImageToolFinal() {
		this.LocalPath=SysArgsTool.getSysArgs().getSysArgs().get("LocalPath");
		this.XD_Path=SysArgsTool.getSysArgs().getSysArgs().get("XiDongPath");
		this.DK_Path=SysArgsTool.getSysArgs().getSysArgs().get("DaKaPath");	
	}
	
	public static ImageToolFinal getInstance() {
		if (instance == null) {
			synchronized (ImageToolFinal.class) {// 1
				if (instance == null) {// 2
					instance = new ImageToolFinal();// 3
				}
			}
		}
		return instance;
	}
	// 单例模式 end
	
	
	public boolean imageDeal(String accessToken){
		try{
		List<com.base.bean.Image> imgList=this.getImagesFromWechat();
		this.downLoad(imgList, accessToken);
		this.mark(imgList);
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
		return true;
		
	}
	
	
	
	//批量下载  最多一次？个 昨天以后的image
	private List<com.base.bean.Image> getImagesFromWechat(){
		ImageDao dao=ImageService.getInstance().getImageDao();
//		System.out.println("from Image a where a.state=0 and a.created>'"+TimeTool.getTodayBegin()+"'");
		List<com.base.bean.Image> imgs=dao.findByHql("from Image a where a.state=0 and a.created>'"+TimeTool.getYestodayBegin()+"'");	
		return imgs;
		
	}
	
	
	private void downLoad(List<com.base.bean.Image> imgList,String accessToken) throws Exception{
		ImageDao dao=ImageService.getInstance().getImageDao();
		String url4p=_url.replace("ACCESS_TOKEN", accessToken);
		for(com.base.bean.Image img:imgList){
			String url=url4p.replace("MEDIA_ID", img.getMediaId());
			
			if(DownImg.download(url, img.getFileName(), LocalPath+"/"+img.getPath()))
				img.setState(1);
			else
				img.setState(0);
			dao.merge(img);
		}
		
	}
	
	
	private void mark(List<com.base.bean.Image> imgList){
		ImageDao dao=ImageService.getInstance().getImageDao();
		for(com.base.bean.Image img:imgList){
			if(img.getState()!=1)
				continue;
			String theFilePath=LocalPath+"/"+img.getPath()+"/"+img.getFileName();	
			WaterMarkUtils.getInstance().mark(theFilePath, theFilePath, Color.blue, img.getDiscription(),img.getTimestr());
			img.setState(2);
			dao.merge(img);
		}		
	}
	

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<com.base.bean.Image> imgs=ImageToolFinal.getInstance().getImagesFromWechat();
		for(com.base.bean.Image img:imgs){
			System.out.println(img.getFileName());
			
			
		}

	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	

}
